#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double r = 6371009;
const double PI = 3.1415926535;
int main(int argc, char** argv) {
    double a,b,c,d;
    double x1,x2,y1,y2,z1,z2,m;
    long long ans;
    int T;
    cin>>T;
    while (T--) {
        cin>>a>>b>>c>>d;
        x1=cos(a);y1=sin(a);z1=sin(b);
        x2=cos(c);y2=sin(c);z2=sin(d);
        m=(x1*x2+y1*y2+z1*z2)/(sqrt(1+z1*z1)+sqrt(1+z2*z2));
        ans=r*(PI*acos(m)-sqrt(2.0-2.0*m));
        printf("%lld\n", ans);
    }
    return 0;
}